package system

import (
	"fmt"
	"k8s-admin/service/system"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

func UserList(ctx *gin.Context) {
	params := new(struct {
		Limit int `form:"limit" binding:"required"`
		Page  int `form:"page" binding:"required"`
	})
	if err := ctx.ShouldBind(&params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
		return
	}
	list, err := system.NewUserInterface().List(params.Limit, params.Page)
	if err != nil {

		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", list)
	}
}

func UserAdd(ctx *gin.Context) {
	param := new(struct {
		Username string `json:"username" binding:"required"`
		Password string `json:"password" binding:"required"`
		Email    string `json:"email"`
		Phone    string `json:"phone"`
		Status   uint   `json:"status"`
		Roles    []uint `json:"roles"`
		DeptId   uint   `json:"dept_id"`
	})
	err := ctx.ShouldBindJSON(&param)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		list, err := system.NewUserInterface().Create(param.Username, param.Password, param.Phone, param.Email, param.Roles, param.Status, param.DeptId)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {

			utils.ReturnContext(ctx).Successful("成功", list)
		}

	}

}
func UserDelete(ctx *gin.Context) {
	// 删除后更改数据库status状态为0，表示账号停用
	param := new(struct {
		UID string `form:"uid" binding:"required"`
	})
	if err := ctx.ShouldBind(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = system.NewUserInterface().Delete(param.UID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {

			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}
}
func UserUpdate(ctx *gin.Context) {
	param := new(struct {
		UID      string `json:"uid"`
		Username string `json:"username" binding:"required"`
		Password string `json:"password"`
		Email    string `json:"email"`
		Phone    string `json:"phone"`
		Status   *uint  `json:"status"`
		Roles    []uint `json:"roles"`
		DeptId   uint   `json:"dept_id"`
	})
	if err := ctx.ShouldBindJSON(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		fmt.Println(param.Status)
		err = system.NewUserInterface().Update(param.UID, param.Username, param.Password, param.Email, param.Phone, *param.Status, param.Roles)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}
}

// 用户权限查询
func UserRole(ctx *gin.Context) {
	param := new(struct {
		UID string `form:"uid" binding:"required"`
	})
	if err := ctx.ShouldBind(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		userInfo, err := system.NewUserInterface().GetUserFromUserName(param.UID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {

			utils.ReturnContext(ctx).Successful("成功", userInfo)
		}
	}
}

func SetUserRole(ctx *gin.Context) {
	param := new(struct {
		UID     string `json:"uid" binding:"required"`
		RolesID []uint `json:"roles_id" binding:"required"`
	})
	if err := ctx.ShouldBindJSON(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = system.NewUserInterface().SetUserRole(param.RolesID, param.UID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {

			utils.ReturnContext(ctx).Successful("成功", "添加角色成功")
		}
	}
}

func DeleteUserRole(ctx *gin.Context) {
	param := new(struct {
		UID     string `json:"uid" binding:"required"`
		RolesID []uint `json:"roles_id" binding:"required"`
	})
	if err := ctx.ShouldBindJSON(&param); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {
		err = system.NewUserInterface().DeleteUserRole(param.RolesID, param.UID)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {

			utils.ReturnContext(ctx).Successful("成功", "角色删除成功")
		}
	}

}
